package com.jubaopen.web.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class URLFilter implements Filter {
	


	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		
	}

	@Override
	public void destroy() {
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse rpo = (HttpServletResponse) response;
		
		Object userId = req.getSession().getAttribute("userId");
		System.out.println(userId);
	
		try {
			req.setCharacterEncoding("utf-8");
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		String serverName = req.getServerName();
		String prefix = serverName.substring(0, 2);
		if (prefix.equals("me") ) {
			String rUrl = "/g"+req.getRequestURI();
			System.out.println(rUrl);
			req.getRequestDispatcher(rUrl).forward(req, rpo);
			return;
		}
		chain.doFilter(request, response);
	}
}